﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HerGoS.Administration.Engine;
using HerGoS.Administration.Entities;
using HerGoS.WebControlExtend;
using HerGoS.ControlsUI;

namespace HerGoS.WebUI
{
    public partial class Persona_Listado : PageBase
    {
        #region Propiedades
        public List<Persona> Personas
        {
            get
            {
                if (ViewState["Personas"] != null)
                    return (List<Persona>)ViewState["Personas"];
                return null;
            }
            set
            {
                ViewState["Personas"] = value;
            }

        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Initialize();
                LoadGridPersonas(false);
            }
        }

        void Initialize()
        {
            this.GridPersonas.Settings(new GridViewExtBasic());
            this.btnNuevo.Settings(new ButtonExtText());
            this.btnRefresh.Settings(new ButtonExtText());
            this.btnFiltrar.Settings(new ButtonExtText());
            this.bntQuitarFiltros.Settings(new ButtonExtText());
        }

        private void LoadGridPersonas(bool cache)
        {
            if (!cache)
                Personas = GestorPersona.Consultar();
            this.GridPersonas.DataSource = Personas;
            this.GridPersonas.DataBind();

        }
        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadGridPersonas(false);
        }

        protected void btnBorrar_Click(object sender, EventArgs e)
        {
            ImageButton btn = (ImageButton)sender;
            int index = Convert.ToInt32(btn.CommandArgument);
            int id = Convert.ToInt32(GridPersonas.DataKeys[index].Value);

            Persona persona = Personas.Find(c => c.OID == id);
            Personas.Remove(persona);

            GestorPersona.Eliminar(persona);
            LoadGridPersonas(true);

        }

        protected void GridPersonas_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButtonExt Editar = e.Row.FindControl("btnEditar") as ImageButtonExt;
                if (Editar != null)
                {
                    Editar.Settings(new ImageButtonExtEdit());
                    int oid = DataBinder.Eval(e.Row.DataItem, "OID").ToInteger();
                    Editar.OnClientClick = "Editar(" + oid.ToString() + "); return false;";
                }

                ImageButtonExt Borrar = e.Row.FindControl("btnBorrar") as ImageButtonExt;
                if (Borrar != null)
                {
                    Borrar.Settings(new ImageButtonExtDelete());
                }
            }
        }

        protected void GridPersonas_PageIndexChanging(Object sender, GridViewPageEventArgs e)
        {
            GridPersonas.PageIndex = e.NewPageIndex;
            LoadGridPersonas(true);
        }

        protected void btnFiltrar_Click(object sender, EventArgs e)
        {
            List<Persona> filtroResult = null;


            if(!string.IsNullOrEmpty(this.txtCuit.Text))
            {  
                filtroResult = Personas.FindAll(y=>y.Cuit == this.txtCuit.Text); 
            }
           
            if(!string.IsNullOrEmpty(this.txtDNI.Text))
            { 
              filtroResult = Personas.FindAll(y=>y.DNI == this.txtCuit.Text);
            
            }

            if (!string.IsNullOrEmpty(this.txtApyNombre.Text))
                filtroResult = Personas.FindAll(y => y.NombresYApellidos.ToLower().Contains(this.txtApyNombre.Text.ToLower()));

            

            this.GridPersonas.DataSource = filtroResult;
            this.GridPersonas.DataBind();
        }
        protected void btnQuitarFiltros_Click(object sender, EventArgs e)
        {
            this.txtApyNombre.Text = string.Empty;
            this.txtDNI.Text= string.Empty;
            this.txtCuit.Text = string.Empty;
            LoadGridPersonas(true);

        }


    }
}